// pages/productDetails/productDetails.js
var aduCartUrl = require('../../config').aduCartUrl;
var app = getApp()
Page({
  data: {
    // banner  的设置
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    // 
    commNum:1,
    // 加入购物车
    sortAnimtead: { },
    animtedata: '',
    boxShow:false,
    payShow:false,
    cartShow:false
  },

    onLoad: function (options) {
      var that = this;
      var showDish= wx.getStorageSync("showDishDetails");
      that.setData({
        showDish: showDish
      })
    },
    // 
    doOrder: function (e) {
      if (e.currentTarget.dataset.type == 'pay'){
        this.setData({
          payShow: true,
          cartShow:false
        });
      } else if (e.currentTarget.dataset.type == 'cart'){
        this.setData({
          payShow: false,
          cartShow: true
        });

      }
      this.setData({
        boxShow: true,
      });
      var animation = wx.createAnimation({
        duration: 800,
        timingFunction: 'ease',
      });
      this.animation = animation;
      animation.bottom('0').opacity(1).step();
      this.setData({
        animtedata: animation
      })
    },
    // 关闭
    closeBox: function () {
      var that = this;
      var animation = wx.createAnimation({
        duration: 800,
        timingFunction: 'ease',
      });
      this.animation = animation;
      animation.bottom('-500rpx').opacity(0).step();
      that.setData({
        animtedata: animation,
        commNum: 1
      })
      setTimeout(function () {
        that.setData({
          boxShow: false
        })
      }, 500)
    },
    /**
     *加减 
     */
    operNum: function (e) {
      var that = this;
      var oper = e.currentTarget.dataset.oper
      var num;
      if (oper == '-') {
        that.data.commNum == 1 ? num = 1 : num = that.data.commNum - 1
      } else {
        num = that.data.commNum + 1;
      }
      that.setData({
        commNum: num
      })
    },
    // 添加购物车
    addCart: function (e,state) {
      var that = this;
      var id = e.currentTarget.dataset.id;
      wx.request({
        method: 'post',
        url: aduCartUrl,
        data: {
          skey: wx.getStorageSync("skey"),
          dishId: id,
          dishNum: that.data.commNum,
          state: 0
        },
        success: function (res) {
          if (res.data.code === 'ok') {
            that.closeBox()
            wx.navigateTo({
              url: '../cart/cart',
            })
          }
        }
      })
    },
    // 立即支付
    pay:function(e){
      var that = this;
      var payList = {};
      wx.request({
        method: 'post',
        url: aduCartUrl,
        data: {
          skey: wx.getStorageSync("skey"),
          dishId: that.data.showDish.dish.id,
          dishNum: that.data.commNum,
          state: 3
        },
        success: function (res) {
          if(res.data.code == "ok"){
            that.closeBox()
            payList = {
              shopList:[{
                cart: res.data.addCart,
                cover: that.data.showDish.icon,
                dish: that.data.showDish.dish
              }],
              total: that.data.commNum * that.data.showDish.dish.price
            }
            wx.setStorageSync("payList", payList);
            wx.navigateTo({
              url: '../payment/payment?type=0'
            })
          }
        }
      });
      //缓存支付列表 
    },
    navTo:function(){
      app.pageTurns("../cart/cart")
    }
})